<template>
    <div class="r_main">
        <div class="tabIn">
            <div class="tab">
                申请广告位
            </div>
            <!-- <div v-if="path == '/promotion/advert'" class="btn2">
                申请广告位
            </div> -->
        </div>
        <div class="form_con">
            <div class="outer" v-if="!$store.getters.isMobile">
                <el-table border :data="list" style="width: 100%">
                    <el-table-column prop="apage_name" align="center" label="广告位置" width="180">
                    </el-table-column>
                    <el-table-column prop="type_name" align="center" label="广告类型" width="180"></el-table-column>

                    <el-table-column align="center" label="广告位置示意图">
                        <template slot-scope="scope">
                            <el-image v-if="scope.row.image" lazy style="max-width: 200px; max-height: 200px"
                                :src="scope.row.image" :preview-src-list="[scope.row.image]">
                            </el-image>
                            <span v-else>暂无图片</span>
                        </template>
                    </el-table-column>
                    <!-- <el-table-column prop="price" align="center" label="广告价格">
                    </el-table-column> -->

                    <el-table-column prop="address" align="center">
                        <template slot-scope="scope">
                            <el-button round type="primary" @click="handleEdit(scope.row.id)">申请</el-button>
                        </template>
                    </el-table-column>
                </el-table>
            </div>
            <div class="outer_mob" v-else>
                <div class="info" v-for="item in list" :key="item.id">
                    <div>
                        <span>广告位置</span>
                        <span>{{ item.apage_name }}</span>
                    </div>
                    <div>
                        <span>广告类型</span>
                        <span>{{ item.type_name }}</span>
                    </div>
                    <div>
                        <span>广告位置示意图</span>
                       <div>
                        <el-image lazy v-if="item.image" style="max-width: 200px; max-height: 200px"
                                :src="item.image" :preview-src-list="[item.image]">
                            </el-image>
                            <span v-else>暂无图片</span>
                       </div>
                    </div>
                
                    <div class="btm">
                        <div>
                            <el-button round type="primary" @click="handleEdit(item.id)">申请</el-button>
                        </div>
                    </div>

                </div>
                <div v-if="list.length == 0">
                    <el-empty description="没有内容了"></el-empty>
                </div>
            </div>
        </div>
        <el-dialog :visible.sync="dialogVisible" :width="$store.getters.isMobile?'90%':'700px'" :close-on-click-modal="false" @close="onclose">
            <span slot="title" class="dialog-header">
                <i style="color:#d41f19;margin-right: 12px;" class="iconfont">&#xee0e;</i>广告申请
            </span>
            <el-form ref="form" label-width="9.37vw">
                <el-form-item label="广告主题:">
                    <el-input type="text" v-model="form.name" placeholder="请输入广告主题"></el-input>
                </el-form-item>
                <el-form-item label="广告链接:">
                    <el-input type="text" v-model="form.url" placeholder="请输入广告链接"></el-input>
                </el-form-item>
                <el-form-item label="购买时长:">
                    <el-select filterable v-model="form.day" placeholder="请选择时长">
                        <el-option v-for="item in days" :key="item.id" :label="item.name" :value="item.id"></el-option>
                    </el-select>
                </el-form-item>
                <div class="upload">
                    <el-form-item label="开始投放时间:">
                        <el-date-picker v-if="!$store.getters.isMobile" prefix-icon="" value-format="yyyy-MM-dd" v-model="form.start_time" type="date"
                            placeholder="选择日期">
                        </el-date-picker>
                       <input type="date" v-model="form.start_time" v-else
                       placeholder="选择日期">
                    </el-form-item>
                </div>
                <el-form-item label="附件:">
                    <div class="uploads">
                        <label>
                            <input type="file" @change="onUpload_file">
                            <span v-if="filename" style="font-size: 16px;color: #808080;line-height: 20px;">{{
                                filename }}(点击上传)</span>
                            <div v-else>
                                选择附件
                            </div>
                        </label>

                    </div>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button type="primary" @click="onsubmit">提交申请</el-button>
            </span>
        </el-dialog>
    </div>

</template>
<script>
import { baseUrl } from '@/apis/index'
import axios from 'axios';
import t1 from "@/assets/img/t1.png"
export default {
    data() {
        return {
            list: [],
            totalPage: 1,
            pageNum: 1,
            loading: false,
            tableData: [],
            dialogVisible: false,
            form: {
                id: '',
                name: '',
                url: '',
                start_time: ''
            },
            days: [],
            url: '',
            filename: ''
        }
    },
    computed: {
        path() {
            return this.$route.path
        }
    },
    mounted() {
        window.addEventListener('scroll', this.handleScroll); // 监听滚动事件
        this.getList()
        this.getDays()


    },
    beforeDestroy() {
        window.removeEventListener('scroll', this.handleScroll); // 移除监听
    },

    methods: {
        getDays() {
            this.$api.getDays().then(res => {
                console.log(res)
                if (res.data.code == 1) {
                    this.days = res.data.data
                }
            })
        },

        getList() {
            this.loading = true
            let params = {
                page: this.pageNum,
            }
            this.$api.getAdv(params).then(res => {
                if (res.data.code == 1) {
                    this.totalPage = res.data.data.last_page
                    this.list = this.list.concat(res.data.data.data);
                    this.loading = false
                }

            })
        },
        //批量导入 / 文件上传
        onUpload_file: function (e) {
            var that = this;
            var file = e.target.files[0];
            var formdata = new FormData();
            formdata.append("file", file, file.name) //通过append向form对象添加
            let config = {
                headers: {
                    "Content-Type": "multipart/form-data",
                    'xx-token': localStorage.getItem('token'),
                    'xx-device-type': 'web'
                }
            }
            axios.post(baseUrl + "user/upload/file", formdata, config).then(function (res) {
                that.$message({
                    message: res.data.msg
                })
                that.filename = res.data.data.filename
                that.url = res.data.data.filepath
                that.txt = res.data.data.url
            }).catch(function (error) {
                that.$message({
                    message: '请求异常'
                })
                console.log("error", error);
                return false;
            })
        },

        handleEdit(id) {
            this.form.id = id
            this.dialogVisible = true
        },

        confirm(item) {

        },
        load() {
            console.log("处理触底");
            var page = this.pageNum
            //如果请求未结束就返回不连续请求
            if (this.loading) {
                return
            }
            //数据请求完成无更多数据时
            if (page >= this.totalPage) {
                return false;
            }
            page = parseInt(page) + 1
            this.pageNum = page
            this.getList()
        },
        onsubmit() {
            this.form.file = this.url
            let params = this.form
            this.$api.applyAdv(params).then(res => {
                if (res.data.code == 1) {
                    this.$message({
                        message: res.data.msg,
                        type: 'success'
                    })
                    this.dialogVisible = false
                    this.onclose()
                    this.$router.back()

                }
            })
        },
        onclose() {
            this.form.day = ''
            this.form.name = ''
            this.form.url = ''
            this.form.file = ''
            this.form.id = ''
            this.form.start_time = ''
            this.filename = ''
        },
        handleScroll() {
            const scrollTop = document.documentElement.scrollTop || document.body.scrollTop;
            const windowHeight = window.innerHeight;
            const scrollHeight = document.documentElement.scrollHeight || document.body.scrollHeight;
            // 判断是否滚动到底部
            if (scrollTop + windowHeight >= scrollHeight - 20) {
                this.load()
            }
        },
    }
}
</script>
<style lang="scss" scoped>
.r_main {
    width: 100%;

    .user {
        width: 100%;
        height: 200px;
        display: flex;
        justify-content: space-between;
        align-items: center;
        position: relative;
        padding: 0 30px;
        overflow: hidden;
        background: linear-gradient(to right, rgba(0, 0, 0, 1), rgba(0, 0, 0, 0));

        .bgg {
            position: absolute;
            width: 100%;
            height: auto;
            z-index: -1;

        }

        .left {
            display: flex;
            color: #fff;
            align-items: center;

            .avatar {
                width: 150px;
                height: 150px;
                margin-right: 27px;
                flex-shrink: 0;
                border-radius: 50%;
                overflow: hidden;

                img {
                    object-fit: cover;
                    width: 100%;
                    height: 100%;
                }
            }

            .info {
                .nane {
                    font-weight: bold;
                    font-size: 30px;
                    color: #FFFFFF;
                    margin-bottom: 13px;
                }

                .data {
                    display: flex;
                    align-items: center;
                    margin-bottom: 13px;

                    .line {
                        height: 18px;
                        display: block;
                        width: 2px;
                        background-color: #fff;
                        margin: 0 18px;
                    }

                    >div {
                        font-weight: 400;
                        font-size: 18px;
                        color: #999999;

                        >span {
                            color: #FFFFFF;
                        }
                    }
                }

                .address {
                    display: flex;
                    align-items: center;
                    margin-bottom: 10px;
                    font-weight: 400;
                    font-size: 16px;
                    color: #999999;

                    .line {
                        margin: 0 18px;
                        display: block;
                        height: 16px;
                        width: 2px;
                        background-color: #999;
                    }
                }

                .sign {
                    font-weight: 400;
                    font-size: 16px;
                    color: #FFFFFF;
                }
            }
        }

        .right {
            height: 100%;
            padding: 28px 0 38px;
            font-weight: 400;
            display: flex;
            justify-content: space-between;
            align-items: flex-end;
            flex-direction: column;
            font-size: 20px;
            color: #FFFFFF;

            .share {
                cursor: pointer;
                display: flex;
                align-items: center;
                justify-content: center;

                img {
                    width: 28px;
                    height: auto;
                    margin-left: 11px;
                }
            }

            .btn {
                display: flex;
                align-items: center;
                justify-content: center;

                button {
                    display: flex;
                    align-items: center;
                    justify-content: center;
                    width: 120px;
                    height: 48px;
                    background: #F98951;
                    border-radius: 24px;
                    opacity: 0.9;
                    margin-left: 26px;
                    font-weight: 400;
                    font-size: 20px;
                    color: #FFFFFF;
                    border: none;

                    &:nth-child(2) {
                        background: #1068E7;
                    }
                }
            }
        }
    }

    .tabIn {
        display: flex;
        align-items: center;
        justify-content: space-between;
        padding: 23px 0;
        background-color: #E5E5E5;
        position: sticky;
        top: 99px;
        z-index: 99;
        padding: 0 23px;
        height: 60px;

        // border-top: 0.05rem solid #E8E8E8;
        .tab {
            font-weight: bold;
            font-size: 24px;
            color: #1068E7;
        }

        .btn2 {
            cursor: pointer;
            font-weight: 400;
            font-size: 14px;
            color: #1068E7;
            background: #FFFFFF;
            border-radius: 4px;
            border: 1px solid #1068E7;
            padding: 8px 10px;
        }

    }

    .form_con {
        min-height: 500px;
        // overflow-y: auto;
    }
}

.outer {
    margin-top: 30px;
    display: flex;
    align-items: flex-start;
    padding-bottom: 20px;

    ::v-deep .el-table th.el-table__cell {
        background-color: #E7F1FE;
    }

}

.upload {
    ::v-deep .el-input__inner {
        padding: 0 35px;
    }

    ::v-deep .el-form-item__content {
        height: auto;
    }
}

.dialog-header {
    font-weight: bold;
    font-size: 22px;
}

.dialog-footer {
    font-weight: bold;
    font-size: 22px;
    display: flex;
    justify-content: center;
    align-items: center;

    button {
        margin: 0 50px;
        width: 140px;
        height: 50px;
        font-size: 16px;
        line-height: 50px;
        padding: 0;
    }
}

.uploads {
    display: flex;

    label {
        margin-right: 3rem;
        text-align: center;
        white-space: nowrap;
        cursor: pointer;
        font-size: 1rem;
        color: #1a1a1a;

        input[type=file] {

            display: none;
            opacity: 0;
        }

        >div {
            width: 158px;
            height: 40px;
            border-radius: 4px;
            border: 1px solid #CCCCCC;
            text-align: center;
            font-weight: 400;
            font-size: 16px;
            color: #999999;
            line-height: 40px;
        }

    }
}
@media screen and (max-width:768px) {

.r_main .tabIn {
    height: 40px;
    padding: 0 20px;

    .tab {
        font-size: 15px;
    }

    .btn2 {
        font-size: 12px;
        width: 60px;
        height: 20px;
        line-height: 20px;
    }
}

.outer_mob {
    padding: 10px 20px;

    .info {
        border: 1px solid #ccc;
        border-radius: 10px;
        padding: 10px;
        margin-bottom: 10px;

        >div {
            display: flex;
            align-items: center;
            flex-wrap: wrap;
            margin-bottom: 5px;

            &:last-child {
                margin: 0;
            }
>div{
    font-size: 13px;
}
            >span {
                display: inline-block;
                font-size: 13px;

                &:first-child {
                    font-size: 14px;
                    font-weight: bold;
                    width: 120px;
                }
            }
        }

        .btm {
            font-size: 12px;
            >div {
                width: 100%;
                display: flex;
                justify-content: center;
                margin-top: 20px;
                >div{
                    border: 1px solid #ccc;
                    padding: 5px 10px;
                }
            }
        }

        ::v-deep .el-button {
            font-size: 12px;
            padding: 5px 10px;
        }

    }
}}
</style>